/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
 */

package res.players;

import java.util.HashMap;

import org.jbox2d.common.Vec2;

import plar.core.Action;

import plar.core.ActionCharacterStatus;
import plar.core.KeyFlag;
import plar.core.Sprite;
import plar.core.ElementPlayer;
import plar.core.Common;
import plar.core.SpriteSet;

/**
 * Squeak is a character, so it can handle user input.
 */
public class Squeak extends ElementPlayer {
    public class SpriteManager extends Action {
        int firing = 0;
        int index = 0;
        int maxFiring = 50;
        HashMap<String, SpriteSet> spriteSet;

        public SpriteManager() {
            spriteSet = new HashMap<String, SpriteSet>();
            spriteSet.put("move_right", new SpriteSet());
            spriteSet.put("move_left", new SpriteSet());
            spriteSet.put("stop_right", new SpriteSet());
            spriteSet.put("stop_left", new SpriteSet());
            spriteSet.put("fire_left", new SpriteSet());
            spriteSet.put("fire_right", new SpriteSet());
            spriteSet.put("down_left", new SpriteSet());
            spriteSet.put("down_right", new SpriteSet());

            String p = "res/players/squeak/";
            spriteSet.get("stop_right").add(new Sprite(p + "squeakr.png", 0.1f));
            spriteSet.get("stop_left").add(new Sprite(p + "squeakl.png", 0.1f));
            spriteSet.get("move_right").add(new Sprite(p + "squeakmr0.png", 0.1f));
            spriteSet.get("move_right").add(new Sprite(p + "squeakmr1.png", 0.1f));
            spriteSet.get("move_right").add(new Sprite(p + "squeakmr2.png", 0.1f));
            spriteSet.get("down_right").add(new Sprite(p + "squeakdr.png", 0.1f));
            spriteSet.get("down_left").add(new Sprite(p + "squeakdl.png", 0.1f));
            spriteSet.get("move_left").add(new Sprite(p + "squeakml0.png", 0.1f));
            spriteSet.get("move_left").add(new Sprite(p + "squeakml1.png", 0.1f));
            spriteSet.get("move_left").add(new Sprite(p + "squeakml2.png", 0.1f));
            spriteSet.get("fire_left").add(new Sprite(p + "squeakfl.png", 0.1f));
            spriteSet.get("fire_right").add(new Sprite(p + "squeakfr.png", 0.1f));
        }

        public void run() {

            boolean isRunning = (getSpeed().x != 0);

            if (firing > maxFiring)
                firing = 0;
            String spriteSuffix;
            if (directionX < 0)
                spriteSuffix = "_left";
            else
                spriteSuffix = "_right";

            String spriteName = new String();

            if (firing > 0 || isFiring) {
                spriteName = "fire";
                firing++;
            }
            // else if (isJumping ) spriteName = "jump";
            else if (isBending)
                spriteName = "down";
            else if (isRunning && !isJumping)
                spriteName = "move";
            else if (!isRunning && !isJumping)
                spriteName = "stop";

            else
                spriteName = "move";
            spriteName += spriteSuffix;

            currentSpriteSet = spriteSet.get(spriteName);
        }

    }

    public int specialClones;

    public Squeak() {
        super();
        actions.addAction("actionManager", new SpriteManager());
        description = "use the SPECIAL key to invade the planet with littly littly squaky";
        name = "Squeaky Duck";
        specialClones = 10;
    }

    public void specialMove(KeyFlag k) {

        k.del(KeyFlag.SPECIAL);

        for (int i = 0; i < specialClones; i++) {
            Squeak s = new Squeak();
			Common.PlayerSpawnInfo asi = new Common.PlayerSpawnInfo();
			asi.energy=1;
			asi.guns=s.gl;
			
            s.actions.addAction("AI", new ActionMoronAI());

            s.actions.forceAction("status", asi );
            ((ActionCharacterStatus) s.actions.getAction("status")).respawn = false;
            double t = Math.random() * 6.24;
            float sin = (float) Math.sin(t);
            float cos = (float) Math.cos(t);
            s.setPosition(sin * 2 * this.getSize().x + this.getPosition().x,
                          cos * 2 * this.getSize().y + this.getPosition().y);

            level.addElement(s);

            s.gl.add(new res.guns.GunQueen());
            s.gl.get(0).ammo = 10;
            s.gl.get(0).owner = s;
            s.applySpeed(new Vec2(sin * 20, cos * 20));
            s.lifeTime=5;
            s.energy=1;


        }
        energy -= 50;

    }

    class ActionMoronAI extends Action {
        public void run() {
            ElementPlayer p = (ElementPlayer) me;
            int r = (int) (Math.random() * 250);
            switch (r) {
            case 1:

                p.actions.forceAction("input", new KeyFlag(KeyFlag.LEFT));
                break;

            case 2:

                p.actions.forceAction("input", new KeyFlag(KeyFlag.RIGHT));
                break;
            case 3:
            case 9:
            case 8:
            case 7:
                p.actions.forceAction("input", new KeyFlag(KeyFlag.FIRE));
                break;
            case 4:

                p.actions.forceAction("input", new KeyFlag(KeyFlag.JUMP));
                break;
            case 5:
                p.actions.forceAction("input", new KeyFlag(KeyFlag.NONE));
            case 6:
                double t = Math.random();
                p.directionX = (float) Math.sin(t);
                p.directionY = (float) Math.cos(t);
                break;
            }
            if (p.energy < 0)
                p.level.delElement(p);
        }
    }

}
